package com.zlx.controller;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.Method;

/**
 * @Author 周罗校
 * @Description 基础的servlet,反射实现框架封装
 * @ClassName BaseServlet
 * @date 2020.09.22 11:40:04
 * 该版权归周罗校所有，最终解释权以官方为准
 */
public class BaseServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取请求地址
        String uri = request.getRequestURI();
        //切割地址
        String[] split = uri.split("/");
        //获取方法名
        String method = split[split.length - 1];
        //反射获取字节对象
        Class aClass = this.getClass();

        try {
            //获取执行方法
            Method declaredMethod = aClass.getDeclaredMethod(method, HttpServletRequest.class, HttpServletResponse.class);
            //暴力反射
            declaredMethod.setAccessible(true);
            //执行方法
            declaredMethod.invoke(this,request,response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
